<?php

namespace app\common\model;

use think\Exception;
use think\Model;
use think\Db;

/**
 * 用户银行卡
 */
class UserBank Extends Model
{
//    protected $hidden=['username','password'];
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';


    public function getBank(){
        return $this->hasOne("Bank","banking_id","id");
    }

    /**
     * H5端添加提现银行账号
     *
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function Haddbankaccount($id=0,$data=array()){
       try{
           if(!empty($id)){
               Db::name('user_bank')->where(['id'=>$id])->update($data);
           }else{
               Db::name('user_bank')->insert($data);
           }
           $status=true;
       }catch (\Exception $e){
           $status=false;
       }
       return $status;
    }

    /**
     * H5端获取用户银行账号列表
     *
     * @return string
     */
    public function bankingListSelect(){
        try{
            $banking=Db::name('banking')->select();
            $banking_array='';
            if(!empty($accont['banking_id'])){
                foreach ($banking as $key=>$val){
                    if($val['id']==$accont['banking_id']){
                        $banking_array .= '<option value='.$val['id'].' selected="selected">'.$val['name'].'</option>';
                    }else{
                        $banking_array .= '<option value='.$val['id'].'>'.$val['name'].'</option>';
                    }

                }
            }else{
                foreach ($banking as $key=>$val){
                    $banking_array .= '<option value='.$val['id'].'>'.$val['name'].'</option>';
                }
            }

            if(empty($banking_array)){
                $banking_array = '<option value=0 selected="selected">请选择银行</option>';
            }
        }catch (\Exception $e){
            if(empty($banking_array)){
                $banking_array = '<option value=0 selected="selected">请选择银行</option>';
            }
        }
        return $banking_array;
    }

}
